package com.lhz.services;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhz.dao.ArrangeTableDao;
import com.lhz.dao.DiningTableDao;
import com.lhz.dao.DuiLieDao;
import com.lhz.dao.mybatis.DiningShowMybatis;
import com.lhz.pojos.Appointment;
import com.lhz.pojos.DiningTable;
import com.lhz.pojos.Duilie;
import com.lhz.vo.DuilieVo;

/**
 *                 _ooOoo_
 *                o8888888o
 *                88" . "88
 *                (| -_- |)
 *                O\  =  /O
 *             ____/`---'\____
 *           .'  \\|     |//  `.
 *          /  \\|||  :  |||//  \
 *         /  _||||| -:- |||||-  \
 *         |   | \\\  -  /// |   |
 *         | \_|  ''\---/''  |   |
 *         \  .-\__  `-`  ___/-. /
 *       ___`. .'  /--.--\  `. . __
 *    ."" '<  `.___\_<|>_/___.'  >'"".
 *   | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *   \  \ `-.   \_ __\ /__ _/   .-` /  /
 *====`-.____`-.___\_____/___.-`____.-'======
 *                 `=---='
 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *             佛祖保佑       永无BUG
 */
@Service
@Transactional
public class DiningTableService {

	@Autowired
	private DiningShowMybatis diningmybatis;

	@Autowired
	private DiningTableDao jpadao;
	
	@Autowired
	private DuiLieDao duiliejapdao;//列队
	
	@Autowired
	private ArrangeTableDao arrjpadao;//预约
	/**
	 * 所有 还要留下分页查询的语句
	 * 
	 * @return
	 */
	public Map<String, Object> searchBydiningInfo(int pageNum, int pageSize) {
		Map<String, Object> map = new HashMap<>();
		List<DiningTable> info = diningmybatis.searchBydiningInfo();
		PageHelper.startPage(pageNum, pageSize, true);
		PageInfo<DiningTable> pagelist = new PageInfo<>(info);
		if (info != null) {
			map.put("searchdining", info);
			map.put("total", pagelist.getTotal());
			map.put("rows", pagelist.getList());
		} else {
			map.put("message", "查询餐桌列表有误");
		}

		return map;

	}

	/**
	 * 状态
	 * 
	 * @param status
	 * @return
	 */
	public Map<String, Object> searchByStatus(int status) {
		Map<String, Object> map = new HashMap<>();
		List<DiningTable> info = diningmybatis.searchByStatus(status);
		if (info != null) {
			map.put("searchdining", info);
		} else {
			map.put("message", "查询餐桌列表有误");
		}
		return map;
	}

	/**
	 * 修改餐桌的状态
	 * 
	 * @param tableName
	 * @return
	 */
	public Map<String, Object> upadatediningTable(String tableName) {
		if (tableName != null) {
			Map<String, Object> map = new HashMap<>();
			int a = 1;
			int b = 0;
			DiningTable findByTableName = jpadao.findByTableName(tableName);
			String type = findByTableName.getType();
			DuilieVo duilievo = diningmybatis.searchByDuilieOrderInfo(type);
			if(duilievo!=null) {
				int aptId = duilievo.getN_apt_id();
				Appointment findOne = arrjpadao.findOne(aptId);//唯一
				String mingzi = findByTableName.getTableName();
				findOne.setTableName(mingzi);
				duiliejapdao.delete(duilievo.getDuilie_id());//删除之后进行修改
				diningmybatis.updateDuilieOrderInfo(type);
				map.put("result", "已帮"+findOne.getUser().getPhone()+"安排到"+mingzi+"餐桌");
				return map;
			}
		 
			//餐桌状态从繁忙恢复空闲，先判断队列表里面有没有正在等待该类型的餐桌，如果没有等待的人，就直接恢复空闲。
			if (findByTableName != null) {
				int status = findByTableName.getStatus();
				if (status == 0) {
					findByTableName.setStatus(a);
					map.put("result", "OK");
				}
				if (status == 1) {
					findByTableName.setStatus(b);
					map.put("result", "OK");
				}
				jpadao.save(findByTableName);
			} 
				map.put("message", "修改状态有错误!");

			return map;
		}
		throw new RuntimeException("没有找到这个餐桌ID呀.....");
	}

	/**
	 * 添加
	 * 
	 * @param din
	 * @return
	 */
	public Map<String, Object> searchByAddTable(DiningTable din) {
		Map<String, Object> map = new HashMap<>();
		if (din.getTableName().isEmpty()) {
			map.put("message", "没有输入餐桌名字呢!");
			return map;
		}
		DiningTable dd = new DiningTable();
		DiningTable tableName = jpadao.findByTableName(din.getTableName());
		System.out.println("AAAAAAAAAAAA:" + tableName);
		String tableName2 = din.getTableName();
		System.out.println("BBBBBBBBBBBB:" + tableName2);
		if (tableName == null) {
			dd.setTableName(din.getTableName());
			dd.setStatus(0);
			dd.setType(din.getType());
			DiningTable save = jpadao.save(dd);
			if (save != null) {
				map.put("result", "添加成功!");
			} else {
				map.put("message", "添加失败!");
			}
			return map;

		} else {
			map.put("message", "已存在该餐桌名!");
			return map;
		}
	}

	/**
	 * 餐桌的删除
	 * 
	 * @param tableName
	 * @return
	 */
	public Map<String, Object> delectdiningtable(String tableName) {
		Map<String, Object> map = new HashMap<>();
		if (!tableName.isEmpty()) {
			// 先去数据库根据传参过来的id进行查询
			DiningTable findByTableName = jpadao.findByTableName(tableName);
			if (findByTableName != null) {
				jpadao.delete(findByTableName);
				map.put("result", "删除成功!");
				return map;
			} else {
				map.put("message", "删除失败!");
				return map;
			}
		} else {
			map.put("message", "没有传过来的id参数");
			throw new RuntimeException("没有传过来的的参数呢.....");
		}
	}

	/**
	 * 修改餐桌属性
	 * 
	 * @param tableName
	 * @param type
	 * @return
	 */
	public Map<String, Object> updatediningTableName(String tableName, String type) {
		Map<String, Object> map = new HashMap<>();
		if (tableName.isEmpty()) {
			map.put("message", "没有找到餐桌名称呢!");
			return map;
		}
		if (type.isEmpty()) {
			map.put("message", "没有找到餐桌类型呢!");
			return map;
		}
		DiningTable findByTableName = jpadao.findByTableName(tableName);
		if (findByTableName != null) {
			findByTableName.setType(type);
			jpadao.save(findByTableName);
			map.put("result", "修改餐桌属性成功啦!");
		} else {
			map.put("message", "没有找到对应的餐桌名称!");
		}
		return map;

	}

}
